package com.dfrz.planeclub.utill;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import okhttp3.*;

import java.io.IOException;
import java.util.Map;

/**身份证验证，okhttp冲突短信验证
 * @author PuXiaLing
 * @description: TODO
 * @date 2024/4/15 22:40
 */
public class IdTestUtil {
    public static Map<String, Object> getIdentify(String name,String idNo) throws IOException {

        String url = "https://idenauthen.market.alicloudapi.com/idenAuthentication";
        // 获取appCode链接：https://market.console.aliyun.com/
        String appCode = "b77f56e7fb774009b881899ff76d2e94";
        String identifyString=postData(appCode, url, name, idNo);
        System.out.println(url+""+name+""+idNo);
        Map<String, Object> map = JSON.parseObject(identifyString , new TypeReference<Map<String, Object>>() {});
        return map;
    }

    public static String postData(String appCode, String url, String name, String idNo) throws IOException {
        String result = "";
        RequestBody formBody = new FormBody.Builder().add("name", name).add("idNo", idNo).build();
        Request request = new Request.Builder().url(url).addHeader("Authorization", "APPCODE " + appCode).post(formBody).build();

        Call call = new OkHttpClient().newCall(request);
        Response response = null;
        try {
            response = call.execute();
        } catch (IOException e) {
            System.out.println("execute failed, message:" + e.getMessage());
        }

        assert response != null;
        if (!response.isSuccessful()) {      // 当返回结果发生错误时
            // 状态码为403时一般是套餐包用尽，需续购；注意：续购不会改变秘钥（appCode），仅增加次数
            // 续购链接：https://market.aliyun.com/products/57000002/cmapi025518.html
            System.out.println("request failed----" + "返回状态码" + response.code()  + ",message:" + response.message());
        }
        result = response.body().string();    //此处不可以使用toString()方法，该方法已过期
        return result;
    }
}
